// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/functions/sparksql/aggregates/register.h>

#include <pollux/functions/sparksql/aggregates/average_aggregate.h>
#include <pollux/functions/sparksql/aggregates/bitwise_xor_aggregate.h>
#include <pollux/functions/sparksql/aggregates/bloom_filter_agg_aggregate.h>
#include <pollux/functions/sparksql/aggregates/central_moments_aggregate.h>
#include <pollux/functions/sparksql/aggregates/collect_list_aggregate.h>
#include <pollux/functions/sparksql/aggregates/regr_replacement_aggregate.h>
#include <pollux/functions/sparksql/aggregates/sum_aggregate.h>

namespace kumo::pollux::functions::aggregate::sparksql {
    extern void registerCollectSetAggAggregate(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite);

    extern void registerFirstLastAggregates(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite);

    extern void registerMinMaxAggregates(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite);

    extern void registerMinMaxByAggregates(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite);

    extern void registerModeAggregate(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite);

    void registerAggregateFunctions(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite) {
        registerFirstLastAggregates(prefix, withCompanionFunctions, overwrite);
        registerMinMaxAggregates(prefix, withCompanionFunctions, overwrite);
        registerMinMaxByAggregates(prefix, withCompanionFunctions, overwrite);
        registerBitwiseXorAggregate(prefix, withCompanionFunctions, overwrite);
        registerBloomFilterAggAggregate(
            prefix + "bloom_filter_agg", withCompanionFunctions, overwrite);
        registerAverage(prefix + "avg", withCompanionFunctions, overwrite);
        registerSum(prefix + "sum", withCompanionFunctions, overwrite);
        registerCentralMomentsAggregate(prefix, withCompanionFunctions, overwrite);
        registerCollectSetAggAggregate(prefix, withCompanionFunctions, overwrite);
        registerCollectListAggregate(prefix, withCompanionFunctions, overwrite);
        registerRegrReplacementAggregate(prefix, withCompanionFunctions, overwrite);
        registerModeAggregate(prefix, withCompanionFunctions, overwrite);
    }

    std::vector<std::string> listAggregateFunctionNames() {
        std::vector<std::string> names;
        exec::aggregateFunctions().withRLock([&](const auto &map) {
            names.reserve(map.size());
            for (const auto &function: map) {
                names.push_back(function.first);
            }
        });

        return names;
    }
} // namespace kumo::pollux::functions::aggregate::sparksql
